package App::VisRE;

use 5.010;
use feature ':5.10';

use Moose;
use Moose::Util::TypeConstraints;

enum 'switch' => qw/m i s x g/;
has 'switches' => (
  	is => 'rw',
	isa => 'ArrayRef[switch]',
	default => sub {
	 	return ['i', 'x', 'g'];   
	},
);
has 're_file' => (
  	is => 'rw',
	isa => 'Str',
	default => 're.txt',
);
has 'match_file' => (
  	is => 'rw',
	isa => 'Str',
	predicate => 'has_match_file',
);
has 're' => (
	is => 'rw',
	isa => 'Str',
	predicate => 'has_re',
);
has 'match_string' => (
	is => 'rw',
  	isa => 'Str',
	predicate => 'has_match_string',
);
subtype Match
	=> as HashRef
	=> where {
		(
			exists $_->{name} &&
			exists $_->{start_pos} &&
			exists $_->{end_pos} &&
			exists $_->{match}
		);
	};
has 'matches' => (
  	is => 'rw',
	isa => 'ArrayRef[Match]',
	predicate => 'has_matches',
);

sub do_search {
 	my ($self) = @_;

	if(!$self->has_re || !$self->has_match_string){
		my @err_msg = ();
		push @err_msg, 'regular expression' if !$self->has_re;
		push @err_msg, 'match string' if !$self->has_match_string;

		return 'Missing: ' . join ' and ', @err_msg;
	}

	return 0;
}

sub add_switch {
 	my ($self,$param) = @_;
	return if $param eq '';

	my $switches = $self->switches;
	push @$switches, $param;
}

sub remove_switch {
 	my ($self,$param) = @_;
	return if $param eq '';

	my $switches = $self->switches;
	my @s = grep { $_ ne $param } @$switches;
	$self->switches(\@s);
}

sub save_re_to_file {
    my ($self) = @_;
	
	$self->_prompt_match_file if !$self->has_match_file;

    my $re       = $self->re;
    my $switches = $self->switches;

    my $text = sprintf( "s/%s/%s", $re, $switches );

	$self->_save_file($self->re_file,$text);
}

sub _prompt_match_file {
	die "Override me";
}

sub _save_file {
    my ($self,$filename,$text) = @_;
	return if $filename eq '';

    open my $fh, '>', $filename or die "No such file $!";
    print $fh $text;
    close $fh;
}

sub _read_file {
    my ( $self, $filename ) = @_;
	return if $filename eq '';

    open my $fh, '<', $filename or die "No such file $!";

    my @text = <$fh>;

    close $fh;

    return join '', @text;
}

sub _log {
    my ( $self, $log_text ) = @_;

	say $log_text;
}

1; 

__END__

=pod

=head1 NAME

App::VisRE - An application to visually test regular expressions

=head1 VERSION

This document describes App::VisRE version 0.0.1

=head1 SYNOPSIS

    use App::VisRE;

=head1 DESCRIPTION

This is the base class to create appliations that allow visual testing
of regular expressions. This class stores all the basic information about
a regular expression and is responsible for performing the actual matches.
Requires a sub-class in order to implement the visual part

=head1 METHODS

=head2 do_search

This is the main searching method. Requires a re and a match_string. Returns
a bool indicating whether the search was a success or not. Stores matches
under matches hashref that indicates match number, capture name (if given),
and matching position in the string.

=head2 add_switch

Adds a switch to the currently used switches. If not a valid
switch, does nothing

=head2 remove_switch

Removes switch from currently used switches.

=head2 save_re_to_file

Takes the regular expression the user is currently working with and saves
that regular expression to a file. If not filename has been specified for
this session, prompts for filename (prompt is an abstract class that must
be overridden).

Removes switch from currently used switches.

=head1 DEPENDENCIES

perl510
Moose

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
C<bug-app-visre@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.


=head1 AUTHOR

W. Tyler Gee  C<< <geekout@gmail.com> >>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2008, W. Tyler Gee C<< <geekout@gmail.com> >>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

=cut
